package com.mall.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DLXConfig {
    //订单队列（等过期）
    @Bean
    public Queue queue() {
        Map<String, Object> props = new HashMap<>();
        // 消息的生存时间 10s
        props.put("x-message-ttl", 30000);
        // 设置该队列所关联的死信交换器（当队列消息到期后依然没有消费，则加入死信 队列）
        props.put("x-dead-letter-exchange", "mall.order.dlx.exchange");
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原队列的routingKey
        props.put("x-dead-letter-routing-key", "order.dlx");
        Queue queue = new Queue("mall.order", true, false, false, props);
        return queue;
    }
    //订单队列交换机（等过期）
    @Bean
    public Exchange exchange() {
        DirectExchange exchange = new DirectExchange("mall.order.exchange", true,false, null);
        return exchange;
    }
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with("order").noargs();
    }
    /**
     * 死信交换器
     * @return
     */
    //死信队列
    @Bean
    public Queue queueDlx() {
        Queue queue = new Queue("mall.order.dlx", true, false, false);
        return queue;
    }
    @Bean
    public Exchange exchangeDlx() {
        DirectExchange exchange = new DirectExchange("mall.order.dlx.exchange", true,false, null);
        return exchange;
    }
    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("order.dlx").noargs();
    }
}
